// pages/appointment/appointment.js
import { orderDetail, orderCreate, formatDate, login, getTel} from '../../utils/api.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    tel:'',
    index:0,
    timeIndex:0,
    columns: [],
    columns1: [],
    doctorInfo:{},
    showPop: false,
    showPopTime:false,
    autosize:{
      maxHeight: 125, minHeight: 60
    },
    doctorId:'',
    dateList:[],
    dateIndex:0,
    showPopDate:false,
    currentDate:'',
    currentWeek:''
  },
  showDateup(){
    this.setData({
      showPopDate: true
    })    
  },
  onDateClose(){
    this.setData({ showPopDate: false });
  },
  changeDate(e){
    let weekArr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    this.setData({
      currentDate: e.detail.value,
      currentWeek: weekArr[new Date(e.detail.value).getDay()]
    })
    orderDetail({ id: this.data.doctorId, date: this.data.currentDate }).then(res => {      
      this.setData({
        columns1: res.data.condition.time,
        timeIndex:0
      })

    })
  },
  showTimeup(){    
    this.setData({
      showPopTime: true
    })    
  },
  showPopup(){
    if (this.data.columns.length < 1) {
      wx.navigateTo({
        url: '/pages/addMember/addMember?ref=appointment&tel=' + wx.getStorageSync('tel'),
      })
    } else {
      this.setData({
        showPop:true
      })
    }
  },
  onTimeClose() {
    this.setData({ showPopTime: false });
  },
  onClose() {
    this.setData({ showPop: false });
  },
  changeTime(e) {
    this.setData({
      timeIndex:e.detail.index
    })
  },
  changePatient(e) {
    this.setData({
      index:e.detail.index
    })
  },
  getPhoneNumber(e) {
    getTel(e)
  },
  onsubmit(e) {
    var that=this
    if (that.data.isLogin) {
      if (!that.data.columns1[that.data.timeIndex]) {
        wx.showToast({
          title: '请选择预约时间',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if (that.data.columns.length == 0 || !that.data.columns[that.data.index]['value']) {
        wx.showToast({
          title: '请选择就诊人',
          icon: 'none',
          duration: 2000
        })
        return;
      }     
      let params = {
        id: that.data.doctorId,
        date: that.data.currentDate,
        hour: that.data.columns1[that.data.timeIndex],
        user_patient_id: that.data.columns[that.data.index]['value'],
        content: e.detail.value.content
      }
      if (wx.getStorageSync('successTem')){
        wx.requestSubscribeMessage({
          tmplIds: [wx.getStorageSync('successTem')],
          success(res1) {
            orderCreate(params, 'post').then(res => {
              wx.navigateTo({
                url: '/pages/confirm/confirm?patient=' + that.data.columns[that.data.index]["text"] + '&date=' + that.data.currentDate + '&week=' + that.data.currentWeek
              })

            })
          }
        })
      }else{
        orderCreate(params, 'post').then(res => {
          wx.navigateTo({
            url: '/pages/confirm/confirm?patient=' + that.data.columns[that.data.index]["text"] + '&date=' + that.data.currentDate + '&week=' + that.data.currentWeek
          })

        })
      }
       
     
      
    }else{
      login(true)
    }
    
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    let date = options.date ? options.date : formatDate(new Date())
    let id = options.id
    this.setData({
      doctorId:id,
      currentDate:date
    })
    const weekArr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    orderDetail({ id: this.data.doctorId, date: this.data.currentDate }).then(res => {
      res.data.condition.order_date_list.forEach((item,index)=>{
        if(item==res.data.date){
          this.setData({
            dateIndex: index,
          })
        }
      })
      this.setData({
        doctorInfo:{
          avatar: res.data.avatar,
          name:res.data.name,
          profession_name: res.data.profession_name
        },
        currentDate: res.data.date,
        currentWeek: weekArr[new Date(res.data.date).getDay()],
        columns1: res.data.condition.time,
        dateList: res.data.condition.order_date_list,
      })
      let arr=[]
      res.data.condition.user_patient.forEach((item,key)=>{
        arr.push({
          text: item.name,
          value: item.id
        })
        if (item.id == res.data.default_user_patient_id){
          this.setData({
            index:key
          })
        }
      })
      this.setData({
        columns: arr
      })

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync('isLogin')){
      this.setData({
        isLogin: wx.getStorageSync('isLogin')
      })
    }else{
      this.setData({
        isLogin:false
      })
    }
    if (wx.getStorageSync('tel')) {
      this.setData({
        tel: wx.getStorageSync('tel')
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})